package com.codeoss.module.ir.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.module.ir.dao.IrSequenceMapper;
import com.codeoss.module.ir.model.IrDocType;
import com.codeoss.module.ir.model.IrSequence;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;

@Service
public class IrSequenceService extends BaseService<IrSequenceMapper, IrSequence> implements IIrSequenceService {
	@Autowired
	private IScriptServiceStore scriptServiceStore;
	@Autowired
	private IIrDocTypeService irDocTypeService;
	
	@Override
	@Transactional
	public String getNextSequenceNo(String code) {
		// TODO Auto-generated method stub
		IrSequence irSequence=getIrSequenceByCode(code);
		Integer currentNext=irSequence.getCurrentNext();
		int nextValue=0;
		if(currentNext==null) {
			Integer startNo=irSequence.getStartNo();
			nextValue=startNo;
		}else {
			nextValue=currentNext+irSequence.getIncrementNo();
		}
		
		String startNewType=irSequence.getStartNewType();
		String currentStrDate=irSequence.getCurrentStrDate();
		String nowStrDate=null;
		if("year".equals(startNewType)) {
			nowStrDate=(new java.text.SimpleDateFormat("yyyy")).format(new java.util.Date());
		}else if("month".equals(startNewType)) {
			nowStrDate=(new java.text.SimpleDateFormat("yyyy-MM")).format(new java.util.Date());
		}else if("day".equals(startNewType)) {
			nowStrDate=(new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new java.util.Date());
		}
		if(nowStrDate!=null&&!nowStrDate.equals(currentStrDate)) {
			Integer startNo=irSequence.getStartNo();
			nextValue=startNo;
			irSequence.setCurrentStrDate(nowStrDate);
		}
		irSequence.setCurrentNext(nextValue);
		
		String nextStrValue=nextValue+"";
		long padding=irSequence.getPadding()-nextStrValue.length();
		for(long i=0;i<padding;i++) {
			nextStrValue="0"+nextStrValue;
		}
		String sequenceNo="";
		String prefixExpression=irSequence.getPrefix();
		if(!StringUtils.isEmpty(prefixExpression)) {
			Expression expression=new Expression();
			expression.setScript(prefixExpression);
			expression.setScriptType(Expression.EXPRESSION_TYPE_JAVASCRIPT);
			String prefix=(String)scriptServiceStore.getScriptService(expression).eval(expression.getScript(), null);
			sequenceNo+=prefix;
		}
		sequenceNo+=nextStrValue;
		String suffixExpression=irSequence.getSuffix();
		if(!StringUtils.isEmpty(suffixExpression)) {
			Expression expression=new Expression();
			expression.setScript(suffixExpression);
			expression.setScriptType(Expression.EXPRESSION_TYPE_JAVASCRIPT);
			String suffix=(String)scriptServiceStore.getScriptService(expression).eval(expression.getScript(), null);
			sequenceNo+=suffix;
		}	
		update(irSequence);
		return sequenceNo;
	}

	@Override
	public IrSequence getIrSequenceByCode(String code) {
		// TODO Auto-generated method stub
		QueryWrapper<IrSequence> queryWrapper=new QueryWrapper<IrSequence>();
		queryWrapper.eq("code", code);
		IrSequence irSequence=getOne(queryWrapper);
		return irSequence;
	}

	@Override
	@Transactional
	public String getNextSequenceNo(Long irDocTypeId) {
		// TODO Auto-generated method stub
		IrDocType irDocType=irDocTypeService.getById(irDocTypeId);
		String code=irDocType.getCode();
		return getNextSequenceNo(code);
	}
}